package cn.ac.iscas.dao.mapper.mysql;

import cn.ac.iscas.dao.mapper.dameng.ClobTypeHandler;
import cn.ac.iscas.dao.provider.mysql.MysqlJobMetadataProvider;
import cn.ac.iscas.domain.job.Job;
import cn.ac.iscas.domain.job.JobSearch;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

@Mapper
//使用mysql数据库
@DS("master")
public interface IMysqlJobMetadataMapper {

    @SelectProvider(type = MysqlJobMetadataProvider.class, method = "select")
    @Results({
            @Result(column="_id", property="_id", jdbcType= JdbcType.VARCHAR, id=true),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="executeTime", property="executeTime", jdbcType=JdbcType.DATE),
            @Result(column="status", property="status", jdbcType=JdbcType.VARCHAR),
            @Result(column="duration", property="duration", jdbcType=JdbcType.BIGINT),
            @Result(column="creator", property="creator", jdbcType=JdbcType.VARCHAR),
            @Result(column="createTime", property="createTime", jdbcType=JdbcType.DATE),
            @Result(column="content", property="content", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class),
            @Result(column="executeCount", property="executeCount", jdbcType=JdbcType.INTEGER),
            @Result(column="cron", property="cron", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="runInfo", property="runInfo", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class),
            @Result(column="preJobId", property="preJobId", jdbcType=JdbcType.CHAR)
    })
    List<Job> select(final JobSearch jobSearch);

    @SelectProvider(type = MysqlJobMetadataProvider.class, method = "selectById")
    @Results({
            @Result(column="_id", property="_id", jdbcType= JdbcType.VARCHAR, id=true),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="executeTime", property="executeTime", jdbcType=JdbcType.DATE),
            @Result(column="status", property="status", jdbcType=JdbcType.VARCHAR),
            @Result(column="duration", property="duration", jdbcType=JdbcType.BIGINT),
            @Result(column="creator", property="creator", jdbcType=JdbcType.VARCHAR),
            @Result(column="createTime", property="createTime", jdbcType=JdbcType.DATE),
            @Result(column="content", property="content", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class),
            @Result(column="executeCount", property="executeCount", jdbcType=JdbcType.INTEGER),
            @Result(column="cron", property="cron", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="runInfo", property="runInfo", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class),
            @Result(column="preJobId", property="preJobId", jdbcType=JdbcType.CHAR)
    })
    Job selectById(final String id);

    @SelectProvider(type = MysqlJobMetadataProvider.class, method = "selectCount")
    int selectCount(final String column);

    @SelectProvider(type = MysqlJobMetadataProvider.class, method = "selectPreJob")
    @Results({
            @Result(column="_id", property="_id", jdbcType= JdbcType.VARCHAR, id=true),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="executeTime", property="executeTime", jdbcType=JdbcType.DATE),
            @Result(column="status", property="status", jdbcType=JdbcType.VARCHAR),
            @Result(column="duration", property="duration", jdbcType=JdbcType.BIGINT),
            @Result(column="creator", property="creator", jdbcType=JdbcType.VARCHAR),
            @Result(column="createTime", property="createTime", jdbcType=JdbcType.DATE),
            @Result(column="content", property="content", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class),
            @Result(column="executeCount", property="executeCount", jdbcType=JdbcType.INTEGER),
            @Result(column="cron", property="cron", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="runInfo", property="runInfo", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class),
            @Result(column="preJobId", property="preJobId", jdbcType=JdbcType.CHAR)
    })
    List<Job> selectPreJob(final String id);

    @Select("<script>" +
            "SELECT * FROM jobmetadata WHERE _id IN " +
            "<foreach collection='ids' item='item' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    @Results({
            @Result(column="_id", property="_id", jdbcType= JdbcType.VARCHAR, id=true),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="executeTime", property="executeTime", jdbcType=JdbcType.DATE),
            @Result(column="status", property="status", jdbcType=JdbcType.VARCHAR),
            @Result(column="duration", property="duration", jdbcType=JdbcType.BIGINT),
            @Result(column="creator", property="creator", jdbcType=JdbcType.VARCHAR),
            @Result(column="createTime", property="createTime", jdbcType=JdbcType.DATE),
            @Result(column="content", property="content", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class),
            @Result(column="executeCount", property="executeCount", jdbcType=JdbcType.INTEGER),
            @Result(column="cron", property="cron", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="runInfo", property="runInfo", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class),
            @Result(column="preJobId", property="preJobId", jdbcType=JdbcType.CHAR)
    })
    List<Job> selectByIds(final List<String> ids);

    @Select({
            "SELECT " +
                    " _id, name, reader, writer, transCount, total ,executeTime ,status ,duration, " +
                    " creator, createTime, content, setting, innerId, componentCount, executeCount, cron, description, runInfo, preJobId " +
                    " FROM jobmetadata ORDER BY createTime DESC "
    })
    @Results({
            @Result(column="_id", property="_id", jdbcType= JdbcType.VARCHAR, id=true),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="executeTime", property="executeTime", jdbcType=JdbcType.DATE),
            @Result(column="status", property="status", jdbcType=JdbcType.VARCHAR),
            @Result(column="duration", property="duration", jdbcType=JdbcType.BIGINT),
            @Result(column="creator", property="creator", jdbcType=JdbcType.VARCHAR),
            @Result(column="createTime", property="createTime", jdbcType=JdbcType.DATE),
            @Result(column="content", property="content", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class),
            @Result(column="executeCount", property="executeCount", jdbcType=JdbcType.INTEGER),
            @Result(column="cron", property="cron", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="runInfo", property="runInfo", jdbcType=JdbcType.CLOB, typeHandler = ClobTypeHandler.class),
            @Result(column="preJobId", property="preJobId", jdbcType=JdbcType.CHAR)
    })
    List<Job> selectAll();


    @Insert({
            "INSERT INTO " +
                    " jobmetadata " +
                    " (_id, name, executeTime ,status ,duration, creator , createTime, " +
                    " content, executeCount, cron, description, runInfo, preJobId ) ",
            " VALUES (#{_id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, ",
            " #{executeTime,jdbcType=DATE}, #{status,jdbcType=VARCHAR}, #{duration,jdbcType=BIGINT}, ",
            " #{creator,jdbcType=VARCHAR}, #{createTime,jdbcType=VARCHAR}, #{content,jdbcType=LONGVARCHAR}, ",
            " #{executeCount,jdbcType=INTEGER}, #{cron,jdbcType=VARCHAR}, ",
            " #{description,jdbcType=VARCHAR}, #{runInfo,jdbcType=LONGVARCHAR}, #{preJobId,jdbcType=CHAR})"
    })
    int insert(Job job);

    @Insert({
            "UPSERT INTO " +
                    " jobmetadata " +
                    " (_id, name, executeTime ,status ,duration, creator , createTime, " +
                    " content, executeCount, cron, description, runInfo, preJobId ) ",
            " VALUES (#{_id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, ",
            " #{executeTime,jdbcType=DATE}, #{status,jdbcType=VARCHAR}, #{duration,jdbcType=BIGINT}, ",
            " #{creator,jdbcType=VARCHAR}, #{createTime,jdbcType=VARCHAR}, #{content,jdbcType=LONGVARCHAR}, ",
            " #{executeCount,jdbcType=INTEGER}, #{cron,jdbcType=VARCHAR}, ",
            " #{description,jdbcType=VARCHAR}, #{runInfo,jdbcType=LONGVARCHAR}, #{preJobId,jdbcType=CHAR})"
    })
    int upsert(Job job);

    @Update({
            "UPDATE " +
                    " jobmetadata " +
                    " SET name=#{name,jdbcType=VARCHAR}, " +
                    " total=#{total,jdbcType=BIGINT} ,executeTime=#{executeTime,jdbcType=DATE} ,status=#{status,jdbcType=VARCHAR} ,duration=#{duration,jdbcType=BIGINT}, " +
                    " creator=#{creator,jdbcType=VARCHAR}, createTime=#{createTime,jdbcType=VARCHAR}, content=#{content,jdbcType=LONGVARCHAR}, " +
                    " executeCount=#{executeCount,jdbcType=INTEGER}, cron=#{cron,jdbcType=VARCHAR}, " +
                    " description=#{description,jdbcType=VARCHAR}, runInfo=#{runInfo,jdbcType=LONGVARCHAR}, preJobId=#{preJobId,jdbcType=CHAR} " +
            " WHERE " +
                    " _id=#{_id,jdbcType=VARCHAR}"

    })
    int update(Job job);

    @Update({
            "UPDATE " +
                    " jobmetadata " +
                    " SET status=#{status,jdbcType=VARCHAR} " +
                    " WHERE " +
                    " _id=#{id,jdbcType=VARCHAR}"

    })
    int updateStatus(String id, String status);

    @Update({
            "UPDATE " +
                    " jobmetadata " +
                    " SET duration=#{duration,jdbcType=BIGINT} " +
                    " WHERE " +
                    " _id=#{id,jdbcType=VARCHAR}"

    })
    int updateDuration(String id, Long duration);

    @InsertProvider(type = MysqlJobMetadataProvider.class, method = "insertBatch")
    int insertBatch(@Param("list") List<Job> jobs);


    @Delete({
            " DELETE FROM jobmetadata ",
            " where _id = #{_id,jdbcType=VARCHAR} "
    })
    int deleteByPrimaryKey(String id);

    @Delete("<script>" +
            "DELETE FROM jobmetadata WHERE _id IN " +
            "<foreach collection='ids' item='item' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    @Results(value = { @Result(column = "_id", property = "_id") })
    int deleteBatch(@Param("ids") List<String> ids);

    @Update({
            "UPDATE " +
                    " jobmetadata " +
                    " SET status=#{status, jdbcType=VARCHAR}, " +
                    " duration=#{duration, jdbcType=BIGINT} " +
                    " WHERE " +
                    " _id=#{jobId, jdbcType=VARCHAR}"

    })
    int updateStatusDuration(String jobId, String status, Long duration);

    @Update({
            "UPDATE " +
                    " jobmetadata " +
                    " SET executeTime=#{executeTime, jdbcType=VARCHAR} " +
                    " WHERE " +
                    " _id=#{jobId, jdbcType=VARCHAR}"

    })
    int updateExecuteTime(String jobId, Date executeTime);

}
